package com.gxd.facedemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxd.facedemo.base.Result;
import com.gxd.facedemo.base.Results;
import com.gxd.facedemo.domain.UserFaceInfo;
import com.gxd.facedemo.mapper.MybatisUserFaceInfoMapper;
import com.gxd.facedemo.request.DeleteFaceRequest;
import com.gxd.facedemo.service.UserFaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags={"数据维护"})
@RestController
public class UserListController {

    @Autowired
    MybatisUserFaceInfoMapper userFaceInfoMapper;

    @Autowired
    private UserFaceInfoService userFaceInfoService;


    @ApiOperation(value="获取所有人脸数据")
    @GetMapping("/userInfo")
    public List<UserFaceInfo> getUserInfo()
    {
        List<UserFaceInfo> list = new ArrayList<UserFaceInfo>();
        list = userFaceInfoMapper.findUserFaceInfoList();
        return list;
    }
    /**
     * 根据分组id和名称删除人脸
     */
    @ResponseBody
    @PostMapping("/deleteFaceByName")
    @ApiOperation(value="根据分组id和名称删除人脸")
    public Result<Object> deleteFaceByName(@RequestBody DeleteFaceRequest request) {
        if ("".equals(request.getName())) {
            return Results.newFailedResult("姓名不能为空");
        }
        if (request.getGroupId() == null) {
            return Results.newFailedResult("分组id不能为空");
        }
        List<UserFaceInfo> userList = userFaceInfoService.list(new QueryWrapper<UserFaceInfo>().eq("group_id", request.getGroupId()).eq("name", request.getName()));
        if (userList==null||userList.isEmpty()){
            return Results.newFailedResult("人脸数据不存在，删除失败");
        }
        List<Integer> userIdList = new ArrayList<>();
        for (UserFaceInfo userFaceInfo : userList) {
            userIdList.add(userFaceInfo.getId());
        }
        userFaceInfoService.removeByIds(userIdList);
        return Results.newSuccessResult("删除人脸数据成功");
    }

    /**
     * 删除人脸集合
     */
    @ResponseBody
    @PostMapping("/deleteFaceSet/{groupId}")
    @ApiOperation(value="删除人脸集合")
    public Result<Object> deleteFaceSet(@PathVariable("groupId") Integer groupId) {
        if (groupId == null) {
            return Results.newFailedResult("分组id不能为空");
        }
        int num = userFaceInfoService.count(new QueryWrapper<UserFaceInfo>().eq("group_id",groupId));
        if (num == 0){
            return Results.newFailedResult("当前集合不存在");
        }
        userFaceInfoService.remove(new QueryWrapper<UserFaceInfo>().eq("group_id",groupId));
        return Results.newSuccessResult("删除人脸集合成功");
    }
}
